package com.baosight.sczx.common.zm.basetools.listener;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.dao.Dao;
import com.baosight.sczx.common.zm.cInterface.cf.MessageUtil;
import com.baosight.sczx.zm.cs.domain.ZMCSProductionUnits;

/**
 * 设备检修提醒
 * <p>Title: EquipmentMaintenance.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 严洪浩
 * @version 1.0
 * <pre>Histroy:
 *          2011-11-11 上午02:32:02  严洪浩  Create
 *</pre>
 */
public class EquipmentMaintenance {
	
	private static long runTime = 0;//运行记录时刻
	
	public EquipmentMaintenance(){
	}
	
	private Dao dao;

	public void setDao(Dao dao)
	{
		this.dao = dao;
	}
	
	/**
	 * 根据设备运行时间提醒设备检修
	 * <p>Function: maintenance</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-11-11上午02:51:38 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void maintenance() throws Exception {
		if((new Date().getTime() - runTime) < 1800000) {
			//每半小时检查一次
			return;
		}
		
		//首先获取需要提醒的设备[生产单元]
		Map<String, String> params = new HashMap<String, String>();
		params.put("fdStatus", "在用");
		List result = dao.query("ZMCSProductionUnits.query", params);
		for(int i = 0; i < result.size(); i++) {
			int limit = 0;
			ZMCSProductionUnits pu = (ZMCSProductionUnits)result.get(i);
			String fdOverhaulPeriod = pu.getFdOverhaulPeriod();//检修周期
			String fdOverhaulTime = pu.getFdOverhaulTime();//上次检修时间
			String fdKeyId = pu.getFdKeyId();//生产单元ID

			if(null != fdOverhaulPeriod && !"".equals(fdOverhaulPeriod.trim())) {
				float _value = Float.parseFloat(fdOverhaulPeriod);
				if(_value <= 5) {
					continue;
				}
				else if(_value > 5 && _value <= 10) {
					limit = 1;
				}
				else if(_value > 10 && _value <= 100) {
					limit = 2;
				}
				else if(_value > 100 && _value <= 500) {
					limit = 36;
				}
				else if(_value > 500 && _value <= 1000) {
					limit = 48;
				}
				else {
					limit = 62;
				}
				
				//获取设备运行时间[判断是否需要提醒]
				Map<String, String> _map = new HashMap<String, String>();
				_map.put("fdKeyId", fdKeyId);
				
				List _temp = dao.query("ZMCEEquipmentRunRecord.queryCount", _map);
				if(_temp.size() > 0) {
					Map _m = (HashMap)_temp.get(0);
					
					BigDecimal fdRunTime = _m.get("fdRunTime") == null ? new BigDecimal(0) : (BigDecimal)_m.get("fdRunTime");
					BigDecimal countRunTime = new BigDecimal(fdRunTime.floatValue()/60).setScale(0, BigDecimal.ROUND_HALF_UP);

					if(null == fdOverhaulTime || "".equals(fdOverhaulTime.trim())) {
						fdOverhaulTime = "0";
					}
					
					float _runtime = countRunTime.floatValue() - Float.parseFloat(fdOverhaulTime);//本次运行时长

					float _checkValue = _value - limit;
					
					if(_runtime > 0 && _runtime >= _checkValue) {
						//发送消息
						
						MessageUtil messageUtil = new MessageUtil();
						messageUtil.setDao(dao);
						int sendMessage = messageUtil.insertIntoMessageCache("【检修提醒】 "+pu.getFdName()+"运行"+countRunTime+"小时,需要检修", "ZMCEA0", "", "", 2);
						
						if(sendMessage != 1)
						{
							System.out.println("消息发送失败: "+sendMessage);
						}
						else {
							//更新上次检修时间
							Map _args = new HashMap();
							_args.put("fdOverhaulTime", countRunTime);
							_args.put("fdKeyId", pu.getFdKeyId());
							dao.update("ZMCSProductionUnits.updateOverhaulTime", _args);
						}
					}
				}
			}
		}

		runTime = new Date().getTime();
	}
}
